using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
//using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class DonNghiPhepData: TMSDataBase  {

		public enum TL  {
            BieuMau_DonNghiPhep,
            BieuMau_DonNghiPhep_ChiTiet,

            ListUser
        }

        private string sqlDonNghiPhep =
            "SELECT [SoDNP],[NguoiDeNghi],[NgayDeNghi],[CongViecHienTai],[LyDo], [NguoiNhanBanGiao], [NguoiDuyet], [NoiDungDuyet], [NgayDuyet] " +
            "FROM [BIEUMAU_DONNGHIPHEP]" ;

        private string sqlDonNghiPhepDetail =
            "SELECT [ID],[SoDNP],[NgayNghi],[SoGioNghi],[GhiChu] " +
            "FROM [BIEUMAU_DONNGHIPHEP_CHITIET]" ;

        public DonNghiPhepData()
        {

            ds.Tables.Add("BieuMau_DonNghiPhep");
            ds.Tables.Add("BieuMau_DonNghiPhep_ChiTiet");
            ds.Tables.Add("ListUser");

            DanhMuc.fill(getTable(TL.ListUser), DanhMuc.TLDM.ListUser); 
          
			initData("('')", true);
		}

        public void initData(string strSoDNP, bool firstLoading)
        {
			if (!firstLoading) {
				getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Clear();
				getTable(TL.BieuMau_DonNghiPhep).Clear();
			}
            SQLHelper.Fill(getTable(TL.BieuMau_DonNghiPhep), GlobalVariable.WSInfo.ConnectionString,
                sqlDonNghiPhep + " WHERE [SoDNP] In " + strSoDNP);

            SQLHelper.Fill(getTable(TL.BieuMau_DonNghiPhep_ChiTiet), GlobalVariable.WSInfo.ConnectionString,
                sqlDonNghiPhepDetail + " WHERE [SoDNP] In " + strSoDNP);

			if (firstLoading) {
				// Set primary key(s)
                getTable(TL.BieuMau_DonNghiPhep).PrimaryKey = new DataColumn[] { getTable(TL.BieuMau_DonNghiPhep).Columns["SoDNP"] };
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).PrimaryKey = new DataColumn[] { getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["ID"] };

				// Set relations
                ds.Relations.Add("M_D", getTable(TL.BieuMau_DonNghiPhep).Columns["SoDNP"],
                    getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["SoDNP"]);

                // Set default values
                getTable(TL.BieuMau_DonNghiPhep).Columns["SoDNP"].DefaultValue = "";
                getTable(TL.BieuMau_DonNghiPhep).Columns["NguoiDeNghi"].DefaultValue = GlobalVariable.WSInfo.UserID;
                getTable(TL.BieuMau_DonNghiPhep).Columns["NgayDeNghi"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.BieuMau_DonNghiPhep).Columns["LyDo"].DefaultValue = "";

                //Detail
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["ID"].AutoIncrement = true;
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["ID"].AutoIncrementStep = 1;
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["NgayNghi"].DefaultValue = GlobalVariable.WSInfo.WorkingDate.AddDays(2);
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["SoGioNghi"].DefaultValue = 8;
                getTable(TL.BieuMau_DonNghiPhep_ChiTiet).Columns["GhiChu"].DefaultValue = "";
			}
           
		}

		public bool createNewRow() {
			try {
                initData("('')", false);

                DataRow dr = getTable(TL.BieuMau_DonNghiPhep).NewRow();
                vg.DateForGenerating = WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "NP";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
                dr["SoDNP"] = vg.generateVoucherNo("BieuMau_DonNghiPhep", "[SoDNP]");
                getTable(TL.BieuMau_DonNghiPhep).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                new SimpleMasterDetailUpdater(sqlDonNghiPhep, sqlDonNghiPhepDetail, getTable(TL.BieuMau_DonNghiPhep), getTable(TL.BieuMau_DonNghiPhep_ChiTiet), GlobalVariable.WSInfo.ConnectionString);
                ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
